package com.yh.csx.crm.appapi.controller;

import com.yh.csx.crm.core.entity.CommonResponse;
import com.yh.csx.crm.core.enums.ApprovalTypeEnum;
import com.yh.csx.crm.core.enums.ArchiveCategoryEnum;
import com.yh.csx.crm.core.enums.CooperationModeEnum;
import com.yh.csx.crm.core.enums.TaxesCategoryEnum;
import com.yh.csx.crm.core.exception.BusinessException;
import com.yh.csx.crm.core.model.PageRequest;
import com.yh.csx.crm.service.request.TodoListReq;
import com.yh.csx.crm.service.response.AppApprovalFlowLogResp;
import com.yh.csx.crm.core.util.BeanUtils;
import com.yh.csx.crm.dao.ApprovalMapper;
import com.yh.csx.crm.dao.CustomerSnapshotMapper;
import com.yh.csx.crm.po.Approval;
import com.yh.csx.crm.po.ApprovalAndCustomerSnapshot;
import com.yh.csx.crm.po.CustomerSnapshot;
import com.yh.csx.crm.service.ApprovalService;
import com.yh.csx.crm.service.CustomerService;
import com.yh.csx.crm.service.response.AppCreditTodoResp;
import com.yh.csx.crm.service.response.SnapshotCompareResp;
import com.yh.csx.crm.service.vo.PageWrap;
import com.yh.csx.crm.service.vo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * ApprovalController class
 *
 * @author huyalin
 * @date 2019/2/27
 */
@Api("APP-审批服务")
@RestController
@RequestMapping("/app/approval")
public class AppApprovalController extends BaseController {

    @Autowired
    ApprovalService approvalService;

    @Autowired
    CustomerService customerService;

    @Autowired
    ApprovalMapper approvalMapper;

    @Autowired
    CustomerSnapshotMapper customerSnapshotMapper;


    @ApiOperation("审批过程记录")
    @GetMapping("/log")
    public CommonResponse<AppApprovalFlowLogResp> getApprovalFlowLog(Long approvalId) {
        return super.visit(() -> {
            if (Objects.isNull(approvalId) || approvalId == 0) {
                throw new BusinessException("审批单Id不能为空");
            }
            return approvalService.getAppApprovalFlow(approvalId);
        });
    }

    @ApiOperation("信控审核待审列表")
    @GetMapping("/credit_todo")
    public CommonResponse<PageWrap<AppCreditTodoResp>> getCreditTodo(PageRequest request) {
        return super.visit(() -> {
            User user = new User(super.getUserId(), super.getUserName());
            TodoListReq todoListReq = new TodoListReq();
            todoListReq.setPageNum(request.getPageNum());
            todoListReq.setPageSize(request.getPageSize());
            List<String> approvalTypeCodes = new ArrayList<>();
            approvalTypeCodes.add(ApprovalTypeEnum.CREDIT.getCode());
            approvalTypeCodes.add(ApprovalTypeEnum.TEMP_CREDIT.getCode());
            PageWrap<ApprovalAndCustomerSnapshot> pageWrap = approvalService.getTodoList(user, todoListReq, approvalTypeCodes);

            List<AppCreditTodoResp> pageWrapResult = BeanUtils.convertList(AppCreditTodoResp.class,pageWrap.getResult());
            for (AppCreditTodoResp appCreditTodoResp : pageWrapResult){
                Approval approval = approvalMapper.selectByPrimaryKey(appCreditTodoResp.getApprovalId());
                CustomerSnapshot customerSnapshot = customerSnapshotMapper.selectByPrimaryKey(approval.getAfterSnapshotId());
                appCreditTodoResp.setPhone(customerSnapshot.getPhone());
                appCreditTodoResp.setCustomerStatus(customerSnapshot.getCustomerStatus());
            }
            PageWrap<AppCreditTodoResp> page = new PageWrap<>();
            page.setPageNo(pageWrap.getPageNo());
            page.setPageSize(pageWrap.getPageSize());
            page.setTotalNum(pageWrap.getTotalNum());
            page.setPages(pageWrap.getPages());
            page.setResult(pageWrapResult);
            return page;
        });
    }

    @ApiOperation("客户信息修改审核待审列表")
    @GetMapping("/modify_todo")
    public CommonResponse<PageWrap<ApprovalAndCustomerSnapshot>> getModifyTodo(PageRequest request) {
        return super.visit(() -> {
            User user = new User(super.getUserId(), super.getUserName());
            TodoListReq todoListReq = new TodoListReq();
            todoListReq.setPageNum(request.getPageNum());
            todoListReq.setPageSize(request.getPageSize());
            List<String> approvalTypeCodes = new ArrayList<>();
            approvalTypeCodes.add(ApprovalTypeEnum.MODIFY.getCode());
            return approvalService.getTodoList(user, todoListReq, approvalTypeCodes);
        });
    }


    @ApiOperation("审批单详情")
    @GetMapping("/details")
    public CommonResponse<SnapshotCompareResp> approvalDetails(Long approvalId) {
        return super.visit(() -> {
            SnapshotCompareResp snapshotCompareResp = approvalService.approvalDetails(approvalId);
            snapshotCompareResp.getNewData();
            snapshotCompareResp.getOldData();
            //archive_category档案分类：01法人，02政府
            //cooperation_mode合作模式：01长期客户；02临时客户
            snapshotCompareResp.getNewData().setArchiveCategory(ArchiveCategoryEnum.resolveName(snapshotCompareResp.getNewData().getArchiveCategory()));
            snapshotCompareResp.getOldData().setArchiveCategory(ArchiveCategoryEnum.resolveName(snapshotCompareResp.getOldData().getArchiveCategory()));

            snapshotCompareResp.getNewData().setCooperationMode(CooperationModeEnum.resolveName(snapshotCompareResp.getNewData().getCooperationMode()));
            snapshotCompareResp.getOldData().setCooperationMode(CooperationModeEnum.resolveName(snapshotCompareResp.getOldData().getCooperationMode()));

            snapshotCompareResp.getNewData().setTaxesCategory(TaxesCategoryEnum.resolveName(snapshotCompareResp.getNewData().getTaxesCategory()));
            snapshotCompareResp.getOldData().setTaxesCategory(TaxesCategoryEnum.resolveName(snapshotCompareResp.getOldData().getTaxesCategory()));
            return snapshotCompareResp;
        });
    }

//    @ApiOperation("审批单详情")
//    @GetMapping("/details")
//    public CommonResponse<SnapshotCompareResp> approvalDetails(Long approvalId) {
//        return super.visit(() -> {
//
//            ApprovalDetails approvalDetails = approvalService.approvalDetails(approvalId);
//
//            Customer customer = customerService.getCustomer(approvalDetails.getApproval().getCustomerId());
//
//            CustomerSnapshotResp oldData = BeanUtils.convert(CustomerSnapshotResp.class, customer);
//            CustomerSnapshotResp newData = BeanUtils.convert(CustomerSnapshotResp.class, approvalDetails.getCustomerSnapshot());
//
//            Map<String, String> oldMap = this.objectToMap(oldData);
//            Map<String, String> newMap = this.objectToMap(newData);
//            Iterator<Map.Entry<String, String>> iterator = newMap.entrySet().iterator();
//            iterator.forEachRemaining(stringStringEntry -> {
//                if (oldMap.get(stringStringEntry.getKey()).equals(stringStringEntry.getValue())) {
//                    newMap.remove(stringStringEntry.getKey());
//                    oldMap.remove(stringStringEntry.getKey());
//                }
//            });
//            return new SnapshotCompareResp(approvalId, oldMap, newMap);
//        });
//    }

}

//
//    @ApiOperation("待审客户-无信控-审核详情")
//    @GetMapping("/customer_no_credit_info")
//    public CommonResponse<PageWrap<AppCustomerResp>> customerNoCredit( AppCustomerReq req) {
//        return super.visit(() -> {
//
//            AppCustomerResp appCustomerResp = new AppCustomerResp();
//
//        });
//    }
//
//    @ApiOperation("待审客户-无信控-审核详情-审核通过结果页")
//    @GetMapping("/customer_fruit_info")
//    public CommonResponse<PageWrap<AppCustomerFruitlnfoResp>> customerFruitInfo( AppCustomerFruitInfoReq req){
//        return super.visit(() -> {
//
//            AppCustomerFruitlnfoResp appCustomerResp = new AppCustomerFruitlnfoResp();
//
//        });
//    }
//
//
//
//    /**
//     * 新建信控客户待审批列表
//     * @return
//     */
//    @ApiOperation("待审客户-信控大客户")
//    @GetMapping("/customer")
//    public CommonResponse<PageWrap<AppCreditBigCustResp>> creditBigCustomer( AppCreditBigCustReq req) {
//        return super.visit(() -> {
//            AppCreditBigCustResp approvalCustomerListResp = new AppCreditBigCustResp();
//
//        });
//    }
//
//    @ApiOperation("待审客户-信控大客户-审核详情")
//    @GetMapping("/customer_Info")
//    public CommonResponse<PageWrap<AppCreditBigCustInfoResp>> creditBigCustomerInfo( AppBigCreditInfoReq req) {
//        return super.visit(() -> {
//            AppCreditBigCustInfoResp appCreditBigCustInfoResp = new AppCreditBigCustInfoResp();
//        });
//    }
//
//    @ApiOperation("待审客户-信控大客户-审核详情-拒绝弹框")
//    @GetMapping("/insert_info")
//    public Object insertInfo(@RequestBody AppRefuseInfoReq req) {
//        return super.visit(() -> {
//        });
//    }
//
//    @ApiOperation("待审客户-信控大客户-审核详情-本节点审核详情")
//    @GetMapping("/app_node_info")
//    public CommonResponse<PageWrap<AppNodeInfoResp>> getNodeInfo( AppNodeInfoReq req) {
//        return super.visit(() -> {
//        });
//    }
//
//    @ApiOperation("待审客户-信控大客户-审核详情-流程详情")
//    @GetMapping("/app_processInfo")
//    public CommonResponse<PageWrap<AppProcessInfoResp>> getProcessInfo( AppProcessInfoReq req) {
//        return super.visit(() -> {
//        });
//    }
//
//
//    @ApiOperation("待审客户-审核拒绝")
//    @GetMapping("/app_custome_no")
//    public CommonResponse<PageWrap<AppCustomerNoResp>> appCustomeNo( AppCustomerNoReq req) {
//        return super.visit(() -> {
//        });
//    }
//
//    @ApiOperation("待审客户-审核拒绝-拒绝详情-非信用户")
//    @GetMapping("/app_custome_no_info")
//    public CommonResponse<PageWrap<AppCustomerNoInfoResp>> appCustomeNoInfo( AppCustomeNoInfoReq req) {
//        return super.visit(() -> {
//        });
//    }
//
//
//
//
//    /**
//     * 信控额度调整待审批列表
//     * @return
//     */
//    @ApiOperation("信控申请审核")
//    @GetMapping("/credit")
//    public CommonResponse<PageWrap<ApprovalTodolist>> getApprovalTodoList(AppLimitReviseReq req) {
//        return super.visit(() -> {
//            ApprovalTodolist approvalCustomerListResp = new ApprovalTodolist();
//        });
//    }
//
//    @ApiOperation("审核固定额度调整")
//    @GetMapping("/credit_limit_adjust")
//    public CommonResponse<PageWrap<AppLimitReviseResp>> getCreditLimitAdjust(AppLimitReviseReq req) {
//        return super.visit(() -> {
//            AppLimitReviseResp approvalCustomerListResp = new AppLimitReviseResp();
//
//        });
//    }
//
//    @ApiOperation("审核固定额度调整-同意或拒绝")
//    @GetMapping("/insert_info_limit")
//    public Object insertInfoLimit(AppInsertInfoLimitReq req) {
//        return super.visit(() -> {
//
//        });
//    }
//
//    @ApiOperation("审核固定额度调整-流程详情")
//    @GetMapping("/credit_limit_process")
//    public CommonResponse<PageWrap<AppCreditLimitProcessResp>> creditLimitProcess(AppCreditLimitProcessReq req) {
//        return super.visit(() -> {
//            AppCreditLimitProcessResp approvalCustomerListResp = new AppCreditLimitProcessResp();
//
//        });
//    }
//
//    @ApiOperation("审核临时额度申请")
//    @GetMapping("/temp_credit_details")
//    public CommonResponse<TempCreditApprovalDetailsResp> getTempCreditApprovalDetails(AppTempCreditDetailsReq req) {
//        return super.visit(() -> {
//            TempCreditApprovalDetailsResp tempCreditApprovalDetailsResp = new TempCreditApprovalDetailsResp();
//            return tempCreditApprovalDetailsResp;
//        });
//    }
//
//    @ApiOperation("应收账款")
//    @GetMapping("/receipts_cash")
//    public CommonResponse<AppReceiptCashResp> getReceiptsCash() {
//        return super.visit(() -> {
//        });
//    }
//
//    @ApiOperation("应收账款详情")
//    @GetMapping("/receipts_cash_info")
//    public CommonResponse<AppReceiptCashInfoResp> getReceiptsCash(AppReceiptCashInfoReq req) {
//        return super.visit(() -> {
//        });
//    }
//
//
//    @ApiOperation("信控预警")
//    @GetMapping("/credit_charge")
//    public CommonResponse<AppCeditChargeResp> getReditCharge(AppReditChargeReq req) {
//        return super.visit(() -> {
//        });
//    }
//
//
//    /**
//     * 客户信息修改待审批列表
//     * @return
//     */
//    @ApiOperation("客户信息修改审核")
//    @GetMapping("/inset_info")
//    public CommonResponse<PageWrap<AppRovalCustomerListResp>> insetInfo() {
//        return super.visit(() -> {
//
//        });
//    }
//
//    @ApiOperation("客户信息修改审核详情")
//    @GetMapping("/inset_detailed_info")
//    public CommonResponse<PageWrap<AppDetailedInfoResp>> insetDetailedInfo(AppDetailedInfoReq req) {
//        return super.visit(() -> {
//
//        });
//    }
//
//    @ApiOperation("客户信息修改审核详情-当前节点")
//    @GetMapping("/inset_detailed_info_point")
//    public CommonResponse<PageWrap<AppDetailedInfoPointResp>> insetDetailedInfo(AppDetailedInfoPointReq req) {
//        return super.visit(() -> {
//
//        });
//    }
//
//    @ApiOperation("客户私海")
//    @GetMapping("/personal_index")
//    public CommonResponse<PageWrap<AppPersonalIndexResp>> personalIndex(AppPersonalIndexReq req) {
//        return super.visit(() -> {
//
//        });
//    }
//
//    @ApiOperation("客户私海_管理者")
//    @GetMapping("/personal__admin")
//    public CommonResponse<PageWrap<AppPersonalAdminResp>> personalAdmin(AppPersonalAdminReq req) {
//        return super.visit(() -> {
//
//        });
//    }
//
//    @ApiOperation("客户私海_客户详情")
//    @GetMapping("/personal_customer_Info")
//    public CommonResponse<PageWrap<AppPersonalCustomerInfoResp>> personalCustomerInfo(AppPersonalCustomerInfoReq req) {
//        return super.visit(() -> {
//
//        });
//    }
//
//    @ApiOperation("客户私海_客户详情_菜单")
//    @GetMapping("/personal_customer_Info_contents")
//    public CommonResponse<PageWrap<AppPerCustInfoContentResp>> personalCustomerInfoContents(AppPerCustInfoContentReq req) {
//        return super.visit(() -> {
//
//        });
//
//    }
//    @ApiOperation("客户私海_客户详情_证件详情")
//    @GetMapping("/personal_customer_type_contents")
//    public CommonResponse<PageWrap<AppPerCustContentTypeResp>> personalCustomerTypeContents(AppPerCustContentTypeReq req) {
//        return super.visit(() -> {
//
//        });
//
//    }
//
//    @ApiOperation("客户私海_客户详情_拜访记录")
//    @GetMapping("/personal_customer_visit")
//    public CommonResponse<PageWrap<AppPersonalCustomerVisitResp>> personalCustomerVisit(AppPersonalCustomerVisitReq req) {
//        return super.visit(() -> {
//
//        });
//
//    }
//
//    @ApiOperation("客户私海_客户详情_拜访详情")
//    @GetMapping("/personal_customer_visit_info")
//    public CommonResponse<PageWrap<AppPersonalCustomerVisitInfoResp>> personalCustomerVisit(AppPersonalCustomerVisitInfoReq req) {
//        return super.visit(() -> {
//
//        });
//
//    }
//
//    @ApiOperation("客户私海_客户详情_订单记录")
//    @GetMapping("/personal_customer_order_info")
//    public CommonResponse<PageWrap<AppPersonalCustomerOrderInfoResp>> personalCustomerVisit(AppPersonalCustomerOrderInfoReq req) {
//        return super.visit(() -> {
//
//        });
//
//    }
//
//    @ApiOperation("客户私海_客户详情_订单详情")
//    @GetMapping("/personal_customer_order_careful_info")
//    public CommonResponse<PageWrap<AppPersonalCustomercarefulInfoResp>> personalCustomerVisit(AppPersonalCustomercarefulInfoReq req) {
//        return super.visit(() -> {
//
//        });
//
//    }
//
//    @ApiOperation("客户私海_固额调整申请")
//    @GetMapping("/personal_norm_limit_revise")
//    public CommonResponse<PageWrap<AppPersonalNormLimitReviseResp>> personalNormLimitRevise(AppPersonalNormLimitReviseReq req) {
//        return super.visit(() -> {
//
//        });
//
//    }
//
//    @ApiOperation("客户私海_申请临时额度")
//    @GetMapping("/personal_temporary_limit_revise")
//    public CommonResponse<PageWrap<AppPersonalTemporaryLimitReviseResp>> personalTemporaryLimitRevise(AppPersonalTemporaryLimitReviseReq req) {
//        return super.visit(() -> {
//
//        });
//
//    }
//
//    @ApiOperation("客户私海_未审核")
//    @GetMapping("/personal_not_assess")
//    public CommonResponse<PageWrap<AppPersonalNotAssessResp>> personalNotAssess(AppPersonalNotAssessReq req) {
//        return super.visit(() -> {
//
//        });
//    }
//
//
//    @ApiOperation("客户私海_跟进中")
//    @GetMapping("/personal_follow_up ")
//    public CommonResponse<PageWrap<AppPersonalFollowUpResp>> personalFollowUp(AppPersonalFollowUpReq req) {
//        return super.visit(() -> {
//
//        });
//    }
//}
